/*-------------------------------------------
文件名：flapp_net_slm.c
功能说明：4G模组通讯
作    者：ZLH
创建日期：2024.8.9
备注：
-------------------------------------------*/

#include "flapp_net_slm.h"
#include "flapp_user.h"
#include "flapp_peripheral.h"
#include "flapp_clock.h"
#include "flapp_factory.h"
#include "flapp_display.h"
#include "flapp_flash.h"

#define TIMEOUT_MAX_CNT 						5
#define TIMEOUT_RESET_TMR 					1800	//30分钟，30*60
#define TIME_WAIT_REQ 							0xFFFF
#define RCV_TIME_STAMP 							10			//时间戳
#define RCV_TOKEN_LEN 							64
#define ICCD_MAX_LEN 								20

char EOF_BODY[] = 
{
	0x1A
};

const unsigned char net_connected_req_step[API_HTTP_MAX] = {
													AT_TOKEN_INIT,
													AT_DVC_PARA_INIT,
													AT_GET_TIME_INIT,	
													AT_GET_TOD_MED_INIT,
													AT_GET_VOLUME_INIT,
													AT_GET_RING_LEN_INIT,
													AT_GET_MED_SET_INIT,
													AT_GET_SUB_CHECK_INIT,
													AT_GET_SUB_DATE_INIT,
													AT_RPT_VOLUME_INIT,
													AT_RPT_MED_INIT,
													AT_REF_TOKEN_INIT,
													AT_RPT_CHECK_VIS_INIT,	
													AT_RPT_SEEK_HELP_INIT,								
												};
typedef struct 
{							
	bool bHaveID;													//有无ID标志
	u8 u8LastNetStep;													//用于需要发特定指令的时候同步
	u8 u8DvcVer;															//设备软件版本号
	u8 u8DvcType;															//设备型号
	u8 u8ModCsq;															//模组信号强度
	u8 u8Interval;															//模组信号强度
	u32 u32DvcID;															//模组信号强度
	fldrv_api_status_req_e u8ApiStatus;				//触发设备参数上报触发动作
	fldrv_net_status_e	u8NetSta;							//网络连接状态
	confirm_state	cPlayNetOk;
	u32 u32HttpReqFlag;
	char s8AccessToken[RCV_TOKEN_LEN + 1];				//						
	char s8RefreshToken[RCV_TOKEN_LEN + 1];				//纬度				
	char s8Iccid[ICCD_MAX_LEN + 1];					//ICCID	
}flapp_net_para_t;	//通讯获取到的参数存储，有需要就获取

typedef struct 
{
	uint8_t	u8NetStep;									//通讯步骤列表
	uint16_t	u16Time;									//每一步需等待时长
	char *s8AtSend;											//封装与4G模组通讯字符串
	void (*pFunc)(char *ptr, unsigned short len);
}flapp_uart_send_t;

typedef struct 
{
	u8	u8CurNetStep;												//当前运行到哪一步
	u8	u8CurHttpStep;											//当前http请求处在哪步,指联网成功
	u8	u8CurNetConCount;										//网络连接次数，次数达到一定还连不上就进入休眠
	u8 	u8TimeOutSta;
	u8 	u8TimeOutCount;											//http超时计数，超过5次关闭模组
	u8 	u8AgainRestCnt;											//http模组重启次数
	u16 u16CurRunTime;											//当前步骤运行时间计算，规定时间内没接收到信息重发指令，接收到进入下一步
	u32 u32TimeOutReset;										//http重启计时，超过30分钟开启模组
	flapp_uart_send_t flapp_uart_send;
	char s8AtSendBuf[UART2_TXBUF_SIZE];
}flapp_net_send_t;

static void flapp_parse_get_csq(char *buf, u16 len);
static void flapp_parse_return_ok(char *buf, u16 len);
static void flapp_parse_return_greater(char *buf, u16 len);
static void flapp_parse_access_token(char *buf, u16 len);
static void flapp_parse_get_service_config(char *buf, u16 len);
static void flapp_parse_get_ring_len(char *buf, u16 len);
void flapp_parse_get_med_config(char *buf, u16 len);
static void flapp_parse_report_status(char *buf, u16 len);
static void flapp_parse_return_net_response(char *buf, u16 len);
static void flapp_parse_get_iccid(char *buf, u16 len);

static const flapp_uart_send_t flapp_uart_send[] = 
{	//联网中
	{AT_STEP_TRB            ,10*DELAY_500MS	,AT_SET_TRB					,flapp_parse_return_ok},
	{AT_STEP_GET_ICCID			,10*DELAY_500MS	,AT_GET_ICCID				,flapp_parse_get_iccid},
	{AT_STEP_GET_SGSW				,10*DELAY_500MS	,AT_GET_SGSW				,flapp_parse_return_ok},
	{AT_STEP_SET_APN				,10*DELAY_500MS	,AT_SET_APN					,flapp_parse_return_ok},
	{AT_STEP_CHECH_CPIN				,10*DELAY_500MS	,AT_CHECH_CPIN					,flapp_parse_return_ok},
	{AT_STEP_SET_CGACT			,10*DELAY_500MS	,AT_ACTIVATE_PDP		,flapp_parse_return_ok},
	{AT_STEP_GET_CSQ				,50*DELAY_500MS	,AT_GET_CSQ					,flapp_parse_get_csq},
	{AT_STEP_SET_CSCLK			,1*DELAY_500MS	,AT_SET_CSCLK				,flapp_parse_return_ok},
	//获取token
	{AT_TOKEN_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_TOKEN_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_TOKEN_URL				,10*DELAY_500MS	,HTTP_GET_TOKEN				,flapp_parse_return_ok},
	{AT_TOKEN_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_TOKEN_AUTH			,10*DELAY_500MS	,AT_HTTP_BASIC_AUTH		,flapp_parse_return_ok},
	{AT_TOKEN_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_TOKEN_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_TOKEN_BODY					,flapp_parse_return_ok},
	{AT_TOKEN_ACTION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_TOKEN_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_access_token},
	{AT_TOKEN_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	//上传设备参数
	{AT_DVC_PARA_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_DVC_PARA_CSQ            ,10*DELAY_500MS	,AT_GET_CSQ					,flapp_parse_get_csq},
	{AT_DVC_PARA_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_DVC_PARA_URL				,10*DELAY_500MS	,UPLOAD_MACHINE_PARAM_HTTP				,flapp_parse_return_ok},
	{AT_DVC_PARA_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_DVC_PARA_AUTH			,10*DELAY_500MS	,AT_HTTP_BEARER_AUTH		,flapp_parse_return_ok},
	{AT_DVC_PARA_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_DVC_PARA_BODY_WAIT				,20*DELAY_500MS	,AT_HTTP_DVC_REPORT_BODY					,flapp_parse_return_ok},
	{AT_DVC_PARA_ACTION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_DVC_PARA_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_report_status},
	{AT_DVC_PARA_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	//获取时间
	{AT_GET_TIME_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_TIME_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_TIME_URL				,10*DELAY_500MS	,HTTP_GET_CUR_TIME				,flapp_parse_return_ok},
	{AT_GET_TIME_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_TIME_ACYION			,20*DELAY_500MS	,AT_HTTP_ACT_GET				,flapp_parse_return_net_response},
	{AT_GET_TIME_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_get_service_config},
	{AT_GET_TIME_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	//获取服药计划
	{AT_GET_MED_SET_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_MED_SET_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_MED_SET_URL				,10*DELAY_500MS	,HTTP_GET_MEDICINE_TIME				,flapp_parse_return_ok},
	{AT_GET_MED_SET_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_MED_SET_AUTH			,20*DELAY_500MS	,AT_HTTP_BEARER_AUTH				,flapp_parse_return_ok},
	{AT_GET_MED_SET_ACYION				,20*DELAY_500MS	,AT_HTTP_ACT_GET					,flapp_parse_return_net_response},
	{AT_GET_MED_SET_READ			,20*DELAY_500MS	,AT_HTTP_READ_DATA				,flapp_parse_get_med_config},
	{AT_GET_MED_SET_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	//获取响铃时长
	{AT_GET_RING_LEN_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_RING_LEN_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_RING_LEN_URL				,10*DELAY_500MS	,HTTP_GET_SOUND_LEN				,flapp_parse_return_ok},
	{AT_GET_RING_LEN_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_RING_LEN_AUTH			,20*DELAY_500MS	,AT_HTTP_BEARER_AUTH				,flapp_parse_return_ok},
	{AT_GET_RING_LEN_ACYION				,20*DELAY_500MS	,AT_HTTP_ACT_GET					,flapp_parse_return_net_response},
	{AT_GET_RING_LEN_READ			,20*DELAY_500MS	,AT_HTTP_READ_DATA				,flapp_parse_get_ring_len},
	{AT_GET_RING_LEN_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*获取音量*/
	{AT_GET_VOLUME_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_VOLUME_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_VOLUME_URL				,10*DELAY_500MS	,HTTP_GET_SOUND_VOL				,flapp_parse_return_ok},
	{AT_GET_VOLUME_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_VOLUME_AUTH			,20*DELAY_500MS	,AT_HTTP_BEARER_AUTH				,flapp_parse_return_ok},
	{AT_GET_VOLUME_ACYION				,20*DELAY_500MS	,AT_HTTP_ACT_GET					,flapp_parse_return_net_response},
	{AT_GET_VOLUME_READ			,20*DELAY_500MS	,AT_HTTP_READ_DATA				,flapp_parse_get_service_config},
	{AT_GET_VOLUME_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*获取复诊日期*/ 
	{AT_GET_SUB_DATE_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_SUB_DATE_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_SUB_DATE_URL				,10*DELAY_500MS	,HTTP_GET_LAST_VISIT				,flapp_parse_return_ok},
	{AT_GET_SUB_DATE_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_SUB_DATE_AUTH			,20*DELAY_500MS	,AT_HTTP_BEARER_AUTH				,flapp_parse_return_ok},
	{AT_GET_SUB_DATE_ACYION				,20*DELAY_500MS	,AT_HTTP_ACT_GET					,flapp_parse_return_net_response},
	{AT_GET_SUB_DATE_READ			,20*DELAY_500MS	,AT_HTTP_READ_DATA				,flapp_parse_get_service_config},
	{AT_GET_SUB_DATE_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*获取当天服药记录*/
	{AT_GET_TOD_MED_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_TOD_MED_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_TOD_MED_URL				,10*DELAY_500MS	,HTTP_GET_TODAY_RECODE				,flapp_parse_return_ok},
	{AT_GET_TOD_MED_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_TOD_MED_AUTH			,20*DELAY_500MS	,AT_HTTP_BEARER_AUTH				,flapp_parse_return_ok},
	{AT_GET_TOD_MED_ACYION				,20*DELAY_500MS	,AT_HTTP_ACT_GET					,flapp_parse_return_net_response},
	{AT_GET_TOD_MED_READ			,20*DELAY_500MS	,AT_HTTP_READ_DATA				,flapp_parse_get_service_config},
	{AT_GET_TOD_MED_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*获取当天复诊记录*/
	{AT_GET_SUB_CHECK_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_GET_SUB_CHECK_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_GET_SUB_CHECK_URL				,10*DELAY_500MS	,UPLOAD_SUB_CHK_VISIT_HTTP				,flapp_parse_return_ok},
	{AT_GET_SUB_CHECK_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_GET_SUB_CHECK_AUTH			,20*DELAY_500MS	,AT_HTTP_BEARER_AUTH				,flapp_parse_return_ok},
	{AT_GET_SUB_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_GET_MED_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_SUB_VISIT_BODY					,flapp_parse_return_ok},
	{AT_GET_SUB_CHECK_ACYION				,20*DELAY_500MS	,AT_HTTP_ACT_POST					,flapp_parse_return_net_response},
	{AT_GET_SUB_CHECK_READ			,20*DELAY_500MS	,AT_HTTP_READ_DATA				,flapp_parse_get_service_config},
	{AT_GET_SUB_CHECK_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*上传服用记录*/
	{AT_RPT_MED_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_RPT_MED_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_RPT_MED_URL				,10*DELAY_500MS	,HTTP_UPLOAD_MEDICINE_RECODE				,flapp_parse_return_ok},
	{AT_RPT_MED_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_RPT_MED_AUTH			,10*DELAY_500MS	,AT_HTTP_BEARER_AUTH		,flapp_parse_return_ok},
	{AT_RPT_MED_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_RPT_MED_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_RPT_MED_BODY					,flapp_parse_return_ok},
	{AT_RPT_MED_ACYION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_RPT_MED_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_report_status},
	{AT_RPT_MED_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*上传音量设置*/
	{AT_RPT_VOLUME_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_RPT_VOLUME_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_RPT_VOLUME_URL				,10*DELAY_500MS	,UPLOAD_MACHINE_VOLUME_HTTP				,flapp_parse_return_ok},
	{AT_RPT_VOLUME_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_RPT_VOLUME_AUTH			,10*DELAY_500MS	,AT_HTTP_BEARER_AUTH		,flapp_parse_return_ok},
	{AT_RPT_VOLUME_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_RPT_VOLUME_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_RPT_VOLUME_BODY					,flapp_parse_return_ok},
	{AT_RPT_VOLUME_ACYION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_RPT_VOLUME_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_report_status},
	{AT_RPT_VOLUME_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*上传随访记录*/
	{AT_RPT_CHECK_VIS_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_RPT_CHECK_VIS_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_RPT_CHECK_VIS_URL				,10*DELAY_500MS	,HTTP_CONFIRM_VISIT				,flapp_parse_return_ok},
	{AT_RPT_CHECK_VIS_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_RPT_CHECK_VIS_AUTH			,10*DELAY_500MS	,AT_HTTP_BEARER_AUTH		,flapp_parse_return_ok},
	{AT_RPT_CHECK_VIS_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_RPT_CHECK_VIS_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_CHECK_VIS_BODY					,flapp_parse_return_ok},
	{AT_RPT_CHECK_VIS_ACYION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_RPT_CHECK_VIS_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_get_service_config},
	{AT_RPT_CHECK_VIS_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	/*上传求助*/
	{AT_RPT_SEEK_HELP_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_RPT_SEEK_HELP_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_RPT_SEEK_HELP_URL				,10*DELAY_500MS	,CONFIG_SEEK_HELP				,flapp_parse_return_ok},
	{AT_RPT_SEEK_HELP_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_RPT_SEEK_HELP_AUTH			,10*DELAY_500MS	,AT_HTTP_BEARER_AUTH		,flapp_parse_return_ok},
	{AT_RPT_SEEK_HELP_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_RPT_SEEK_HELP_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_SEEK_HELP_BODY					,flapp_parse_return_ok},
	{AT_RPT_SEEK_HELP_ACYION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_RPT_SEEK_HELP_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_report_status},
	{AT_RPT_SEEK_HELP_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	//刷新token
	{AT_REF_TOKEN_INIT            ,10*DELAY_500MS	,AT_HTTP_INIT					,flapp_parse_return_ok},
	{AT_REF_TOKEN_CID			,10*DELAY_500MS	,AT_HTTP_CID				,flapp_parse_return_ok},
	{AT_REF_TOKEN_URL				,10*DELAY_500MS	,HTTP_GET_TOKEN				,flapp_parse_return_ok},
	{AT_REF_TOKEN_CONTENT				,10*DELAY_500MS	,AT_HTTP_CONTENT					,flapp_parse_return_ok},
	{AT_REF_TOKEN_AUTH			,10*DELAY_500MS	,AT_HTTP_BASIC_AUTH		,flapp_parse_return_ok},
	{AT_REF_TOKEN_BODY				,20*DELAY_500MS	,AT_HTTP_BODY_HEAD					,flapp_parse_return_greater},
	{AT_REF_TOKEN_BODY_WAIT			,20*DELAY_500MS	,AT_HTTP_REF_TOKEN_BODY					,flapp_parse_return_ok},
	{AT_REF_TOKEN_ACTION			,20*DELAY_500MS	,AT_HTTP_ACT_POST				,flapp_parse_return_net_response},
	{AT_REF_TOKEN_READ				,20*DELAY_500MS	,AT_HTTP_READ_DATA					,flapp_parse_access_token},
	{AT_REF_TOKEN_TERM			,1*DELAY_500MS	,AT_HTTP_TERM				,flapp_parse_return_ok},
	
	{AT_STEP_TEST_CSQ				,50*DELAY_500MS	,AT_GET_CSQ					,flapp_parse_get_csq},
	{AT_STEP_TEST_ICCID			,50*DELAY_500MS	,AT_GET_ICCID					,flapp_parse_get_iccid},
};

flapp_net_send_t flapp_net_send;
flapp_net_para_t flapp_net_para;
rtc_time_t	eat_time;	//服药时间
day_time_t sub_day;		//复诊日期
http_control_t	http_ctrl;

void flapp_power_on_net_init(void)
{
	u16 read_buf[4];
	med_time_t time;
	flash_read(FLASH_SET_PARA_ADDR,read_buf,4);	//读取ID
	if(FLASH_SAVE_HEAD_ADDR == (read_buf[0] << 16) + read_buf[1])
	{
		flapp_set_led_sta(DIS_NET,TRUE);
		flapp_net_para.bHaveID = TRUE;
		flapp_net_para.u32DvcID = (read_buf[2] << 16) + read_buf[3];
	}
	else
	{
		flapp_set_led_sta(DIS_NET,2);
		flapp_net_para.bHaveID = FALSE;
		COM_PRINTF("Please write ID!!!");
	}
	
	flash_read(FLASH_MED_LOG_ADDR,read_buf,4);	//读取未上报服药记录
	if(FLASH_SAVE_HEAD_ADDR == (read_buf[0] << 16) + read_buf[1])
	{
		rtc_time_t	time;	//服药时间
		covUnixTimeStp2Beijing(((read_buf[2] << 16) + read_buf[3]),&time);
		http_ctrl.bTodMedInfo = TRUE;
		eat_time = time;
		flapp_set_http_req(API_RPT_MED_TIME,API_STATUS_MAX);							//更新token
		COM_PRINTF("date:%d-%d-%d",eat_time.u16Year,eat_time.u8Mon,eat_time.u8Day);
		COM_PRINTF("time:%d-%d-%d",eat_time.u8Hour,eat_time.u8Min,eat_time.u8Sec);
	}
	else
	{
		http_ctrl.bTodMedInfo = FALSE;
		COM_PRINTF("no med info!!!");
	}
	
	
	time = flapp_get_med_para();
	time.bPlayNetSucc = FALSE;		//开机都将网络连接成功标志清除
	time.bTimeAdj = FALSE;
	http_ctrl.bTimeAdj = FALSE;
	flapp_set_med_para(time);
	
	flapp_net_para.cPlayNetOk = FALSE;
	flapp_net_send.u8TimeOutSta = TIMEOUT_CLR;
	flapp_net_send.u8TimeOutCount = FALSE;
	flapp_net_send.u8AgainRestCnt = FALSE;
	flapp_net_send.u32TimeOutReset = FALSE;
	flapp_net_send.u8CurNetStep = AT_STEP_TRB;										//执行指令重启
	flapp_net_para.u8NetSta = NET_STA_CONNECTED_SUCC;							//联网状态连接中													//
	flapp_net_send.u16CurRunTime = RESET;													//清除当前访问等待时间
	flapp_set_http_req(API_GET_TOKEN,API_STATUS_MAX);							//更新token
	flapp_set_http_req(API_RPT_DEV_PARA,API_POWER_ON);						//上报设备参数
	flapp_set_http_req(API_GET_CURR_TIME,API_STATUS_MAX);					//获取当前时间
	flapp_set_http_req(API_GET_TODAY_MED_LOG,API_STATUS_MAX);			//获取当天服药记录
	flapp_set_http_req(API_GET_VOLUME,API_STATUS_MAX);						//获取音量
	flapp_set_http_req(API_GET_RING_TIME,API_STATUS_MAX);					//获取响铃时长
	flapp_set_http_req(API_GET_MED_TIME,API_STATUS_MAX);					//获取服药计划
	flapp_set_http_req(API_GET_TODAY_SUB_LOG,API_STATUS_MAX);			//获取当天复诊
	flapp_set_http_req(API_GET_SUB_TIME,API_STATUS_MAX);					//获取复诊日期
	fldrv_uart_send_buf(flapp_uart_send[FALSE].s8AtSend,strlen(flapp_uart_send[FALSE].s8AtSend));
}
/**
* @brief  超时时间到了重置模组状态参数
  * @param  none
  * @retval none
  */
void flapp_reset_net_init(void)
{
	flapp_net_send.u8TimeOutSta = TIMEOUT_CLR;
	flapp_net_send.u8TimeOutCount = FALSE;
	flapp_net_send.u8AgainRestCnt = FALSE;
	flapp_net_send.u32TimeOutReset = FALSE;
	flapp_net_send.u8CurNetStep = AT_STEP_TRB;										//执行指令重启
	flapp_net_para.u8NetSta = NET_STA_CONNECTED_SUCC;							//联网状态连接中
	flapp_net_send.u16CurRunTime = RESET;													//清除当前访问等待时间
	fldrv_uart_send_buf(flapp_uart_send[FALSE].s8AtSend,strlen(flapp_uart_send[FALSE].s8AtSend));
}

/**
* @brief  函数用来截取字符串中的某段字符并把字符转换成10进制数值输出
  * @param  Str是源字符串,StopChar为检索到该字符后停止
  * @retval none
  */
u32 flapp_get_dec_num_from_str(char *str, char stopChar)
{
	char * sp = str;
	u32 u32ReVal = 0;

	while (*sp && *sp!=stopChar && *sp>0x2F && *sp<0x3a) //字符串结束或者等于了结束的StopChar,并且保证是0 - 9
	{
		u32ReVal *= 10;
		u32ReVal += *sp-0x30;
		sp++;
	}
	return u32ReVal;
}

static void flapp_timeout_fun(timeout_status_e sta)
{
	flapp_net_send.u8TimeOutSta = sta;
	switch(flapp_net_send.u8TimeOutSta)
	{
		case TIMEOUT_CLR:
			flapp_net_send.u16CurRunTime = TIME_WAIT_REQ;	
			flapp_net_send.u8TimeOutCount = FALSE;
			flapp_net_send.u8AgainRestCnt = FALSE;
			flapp_net_send.u32TimeOutReset = FALSE;
			break;
		case TIMEOUT_ADD_RUN:
			flapp_net_send.u8TimeOutCount++;
			if(flapp_net_send.u8TimeOutCount >= TIMEOUT_MAX_CNT)	//连续连接5次没连上断开模组等待30分钟
			{
				EN_MO_POW_OFF;
				if(flapp_net_send.u8AgainRestCnt == FALSE)	//第一次重连停几秒钟
				{
					flapp_net_send.u8AgainRestCnt = 1;
					flapp_net_send.u8TimeOutCount = FALSE;
					log_debug("Wait 3 sec restart\r\n");
				}
				else																				//第二次重连30分钟等待
				{
					flapp_net_send.u8AgainRestCnt = FALSE;
					flapp_net_para.u8NetSta = NET_STA_CONNECTING;
					flapp_net_send.u8TimeOutSta = TIMEOUT_RES_RUN;
					flapp_net_send.u32TimeOutReset = flapp_get_current_timestp();
					flapp_set_led_sta(DIS_NET,FALSE);
					log_debug("Wait 30 minutes for the module to restart\r\n");
				}
			}
			else
			{
				if(flapp_net_send.u8AgainRestCnt == 1)
				{
					EN_MO_POW_ON;
				}
			}
			break;
		case TIMEOUT_RES_RUN:
			if(flapp_get_current_timestp() - flapp_net_send.u32TimeOutReset >= TIMEOUT_RESET_TMR)
			{
				EN_MO_POW_ON;
				flapp_reset_net_init();
				log_debug("time to restart the module\r\n");
			}
			break;
	}
}

/**
* @brief  解析常规指令返回OK的
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
static void flapp_parse_return_ok(char *buf, u16 len)
{
	if(strstr(buf,"AT+HTTPTERM")){						
		flapp_net_send.u8CurNetStep = NET_WAIT;	
		flapp_timeout_fun(TIMEOUT_CLR);
		CLRBIT_VAL(flapp_net_para.u32HttpReqFlag,flapp_net_send.u8CurHttpStep);
		return;
	}
	
	if(NULL == strstr(buf,"OK"))	return;	
	
	if(flapp_net_send.u8CurNetStep == AT_STEP_SET_CSCLK){
		flapp_net_para.u8NetSta = NET_STA_CONNECTED_SUCC;	
		flapp_net_send.u8CurNetStep = NET_WAIT;						//网络连接成功后等待
		flapp_timeout_fun(TIMEOUT_CLR);
		return;
	}
	flapp_net_send.u8CurNetStep++;
	flapp_timeout_fun(TIMEOUT_CLR);
}	
/**
* @brief  解析常规指令返回OK的
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
static void flapp_parse_return_greater(char *buf, u16 len)
{
	if(NULL == strstr(buf,">"))	return;
	
	flapp_net_send.u8CurNetStep++;			//下一步
	flapp_timeout_fun(TIMEOUT_CLR);
}	

static void flapp_parse_return_net_response(char *buf, u16 len)
{
	if(NULL == strstr(buf," 200 "))	return;
	
	flapp_net_send.u8CurNetStep++;			//下一步
	flapp_timeout_fun(TIMEOUT_CLR);
	flapp_net_para.u8NetSta = NET_STA_CONNECTED_SUCC;	
	flapp_set_led_sta(DIS_NET,TRUE);
}

#ifdef TEST_MODE
volatile u8 test_day = 0;;
#endif
static void  flapp_parse_access_token(char *buf, u16 len)
{
	char *p;
	med_time_t time;
	u32 vaild,token_vaild_end;
	if(strstr(buf,"access_token\":\""))	//token字符串
	{
		time = flapp_get_med_para();
		p = strstr(buf,"\":\"") + strlen("\":\"");	//字符串中获取子字符串出现的位置
		memset(flapp_net_para.s8AccessToken, 0, RCV_TOKEN_LEN);
		strncpy(flapp_net_para.s8AccessToken,p,RCV_TOKEN_LEN);
		flapp_net_para.s8AccessToken[RCV_TOKEN_LEN] = '\0';
		p = strstr(buf,"expires_in\":");
		vaild = flapp_get_dec_num_from_str(p+strlen("expires_in\":"),',');
		p = strstr(buf,"created_at\":");
		token_vaild_end = flapp_get_dec_num_from_str(p+strlen("created_at\":"),'}');
#ifdef TEST_MODE
		time.u32TokenVaild = TEST_TIMESTAMP + (test_day + 7) * 86400;
		COM_PRINTF("TokenVaild_time:%u",time.u32TokenVaild);
#else
		time.u32TokenVaild = vaild + token_vaild_end;
		COM_PRINTF("TokenVaild_time:%u",time.u32TokenVaild);
#endif
		flapp_set_med_para(time);
		p = strstr(buf,"refresh_token\":\"") + strlen("refresh_token\":\"");	//字符串中获取子字符串出现的位置
		memset(flapp_net_para.s8RefreshToken, 0, RCV_TOKEN_LEN);
		strncpy(flapp_net_para.s8RefreshToken,p,RCV_TOKEN_LEN);
		flapp_net_para.s8RefreshToken[RCV_TOKEN_LEN] = '\0';
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
//		log_debug("access_token:%s",flapp_net_para.s8AccessToken);
//		log_debug("refresh_token:%s",flapp_net_para.s8RefreshToken);
	}
}

static void flapp_parse_get_ring_len(char *buf, u16 len)
{
	char *p;
	p = strstr(buf,"\"device_time_length\"");
	if(p){
		http_ctrl.remind_flag = flapp_get_dec_num_from_str(p+strlen("\"device_time_length\":"),'}');
		switch(http_ctrl.remind_flag)
		{
			case 1:http_ctrl.remind_cnt = 6;break;
			case 2:http_ctrl.remind_cnt = 12;break;
			case 3:http_ctrl.remind_cnt = 300;break;
			default:break;
		}
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
//		log_debug("ring_time_len:%d",http_ctrl.remind_cnt);
	}else{
		http_ctrl.remind_flag = FALSE;
		http_ctrl.remind_cnt = FALSE;
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}	
}
/**
* @brief  解析获取平台时间
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
#define SUB_VISIT_DATE					"\"subsequent_visit_date\":\""
static void flapp_parse_get_service_config(char *buf, u16 len)
{
	char *p;
	u32 u32response;
	
	p = strstr(buf,"response\":");
	if(p){
		u32response = flapp_get_dec_num_from_str(p+strlen("response\":"),'}');
		http_ctrl.bTimeAdj = TRUE;		
#ifdef TEST_MODE
		u32response = TEST_TIMESTAMP + test_day * 86400;
		flapp_timestamp_to_datetime((u32)(u32response));
		COM_PRINTF("TIMESTAMP:%u",u32response);
		test_day++;
#else
		flapp_timestamp_to_datetime(u32response);
//		flapp_timestamp_to_datetime(1728489570);
#endif
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}

	p = strstr(buf,"\"volume\"");
	if(p){
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
		flapp_set_volume(flapp_get_dec_num_from_str(p+strlen("\"volume\":"),'}'));
	}

	p = strstr(buf,"\"sub_visit_check_flag\":");
	if(p){
		http_ctrl.bTodReferralInfo = flapp_get_dec_num_from_str(p+strlen("\"sub_visit_check_flag\":"),'}');
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}

	p = strstr(buf,"\"subsequent_visit_date\"");
	if(p){		//有复诊日期
		sub_day.u16Year = flapp_get_dec_num_from_str(p+strlen(SUB_VISIT_DATE),'-');
		sub_day.u8Mon = flapp_get_dec_num_from_str(p+strlen(SUB_VISIT_DATE) + 5,'-');
		sub_day.u8Day = flapp_get_dec_num_from_str(p+strlen(SUB_VISIT_DATE) + 8,'-');
//		log_debug("sub_plan:%d %d %d",sub_day.u16Year,sub_day.u8Mon,sub_day.u8Day);
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
		if(flapp_net_para.cPlayNetOk == FALSE)
		{
			med_time_t time;
			time = flapp_get_med_para();
			time.bPlayNetSucc = TRUE;		//标记网络连接成功语音
			flapp_net_para.cPlayNetOk = TRUE;
			flapp_set_led_sta(DIS_MED,0xff);
			flapp_set_play_command(VOICE_NET_OK,KEY_NUM_ONE);
			fldrv_pro_voice_command();
			flapp_set_med_para(time);
		}
	}

	p = strstr(buf,"\"status\":\"");
	if(p && (flapp_net_send.u8CurNetStep == AT_GET_TOD_MED_READ)){	//当天服药记录
		if(strstr(buf,"\"status\":\"true"))
			http_ctrl.bTodMedInfo = TRUE;
		else
		{
			if(http_ctrl.bTodMedInfo == FALSE)
				http_ctrl.bTodMedInfo = FALSE;
		}
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}
	else if(p && (flapp_net_send.u8CurNetStep == AT_GET_SUB_DATE_READ)){	//当天复诊记录
		sub_day.u16Year = 0;
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
		if(flapp_net_para.cPlayNetOk == FALSE)
		{
			med_time_t time;
			time = flapp_get_med_para();
			time.bPlayNetSucc = TRUE;
			flapp_net_para.cPlayNetOk = TRUE;
			flapp_set_led_sta(DIS_MED,0xff);
			flapp_set_play_command(VOICE_NET_OK,KEY_NUM_ONE);
			fldrv_pro_voice_command();
			flapp_set_med_para(time);
		}
	}
	else if(p && (flapp_net_send.u8CurNetStep == AT_RPT_CHECK_VIS_READ)){	//随访
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
		flapp_set_play_command(VOICE_BEST_WISH,KEY_NUM_ONE);
		fldrv_pro_voice_command();
	}	
}
/**
* @brief  解析服药时间
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
#define BEGIN_DATE						"begin_date\":\""
#define END_DATE							"end_date\":\""
#define SOTP_START_DATE				"start_date\":\""
#define SOTP_END_DATE					"end_date\":\""

void flapp_parse_get_med_config(char *buf, u16 len)
{
	char *p;
	med_time_t time;
	day_time_t day;
	p = strstr(buf,"time\":\"");
	time = flapp_get_med_para();
	if(p){
		time.u8Hour = flapp_get_dec_num_from_str(p+strlen("time\":\""),':');
		time.u8Min = flapp_get_dec_num_from_str(p+strlen("time\":\"")+3,':');
		p = strstr(buf,"interval\":");
		time.u8Interval = flapp_get_dec_num_from_str(p+strlen("interval\":"),',');
		flapp_net_para.u8Interval = time.u8Interval;
		p = strstr(buf,BEGIN_DATE);
		day.u16Year = flapp_get_dec_num_from_str(p+strlen(BEGIN_DATE),'-');
		day.u8Mon = flapp_get_dec_num_from_str(p+strlen(BEGIN_DATE) + 5,'-');
		day.u8Day = flapp_get_dec_num_from_str(p+strlen(BEGIN_DATE) + 8,'-');
		time.u16StartDay = date_to_days(day.u16Year,day.u8Mon,day.u8Day);
		
		p = strstr(buf,END_DATE);
		day.u16Year = flapp_get_dec_num_from_str(p+strlen(END_DATE),'-');
		day.u8Mon = flapp_get_dec_num_from_str(p+strlen(END_DATE) + 5,'-');
		day.u8Day = flapp_get_dec_num_from_str(p+strlen(END_DATE) + 8,'-');
		time.u16EndDay = date_to_days(day.u16Year,day.u8Mon,day.u8Day);
		
		p = strstr(buf,SOTP_START_DATE);
		if(p){
			day.u16Year = flapp_get_dec_num_from_str(p+strlen(SOTP_START_DATE),'-');
			day.u8Mon = flapp_get_dec_num_from_str(p+strlen(SOTP_START_DATE) + 5,'-');
			day.u8Day = flapp_get_dec_num_from_str(p+strlen(SOTP_START_DATE) + 8,'-');
			time.u16StopStrDay = date_to_days(day.u16Year,day.u8Mon,day.u8Day);
			day.u16Year = flapp_get_dec_num_from_str(p+strlen(SOTP_START_DATE) + 24,'-');
			day.u8Mon = flapp_get_dec_num_from_str(p+strlen(SOTP_START_DATE) + 29,'-');
			day.u8Day = flapp_get_dec_num_from_str(p+strlen(SOTP_START_DATE) + 32,'-');
			time.u16StopEndDay = date_to_days(day.u16Year,day.u8Mon,day.u8Day);
		}
		else{
			time.u16StopStrDay = 0;
			time.u16StopEndDay = 0;
		}
		time.bTodMedInfo = http_ctrl.bTodMedInfo;
		time.bTimeAdj = http_ctrl.bTimeAdj;
		time.u16RingLen = http_ctrl.remind_cnt;
		time.u8RingSta =	http_ctrl.remind_flag;
		flapp_set_med_para(time);
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
		//log_debug("med_plan:%d %d %d %d",time.u16StartDay,time.u16EndDay,time.u16StopStrDay,time.u16StopEndDay);
		return;
	}
	p = strstr(buf,"not");
	if(p)	//没有服药计划或没有绑定
	{
		time.u16StartDay = 0xffff;
		time.bTodMedInfo = TRUE;			//没有绑定将当天服药设置为已经服药
		flapp_set_led_sta(DIS_MED,FALSE);		//没有绑定强制关闭服药灯光
		time.bTimeAdj = http_ctrl.bTimeAdj;
		time.u16RingLen = http_ctrl.remind_cnt;
		time.u8RingSta =	http_ctrl.remind_flag;
		flapp_set_med_para(time);						
		flapp_set_med_alarm(MED_TYPE_NO);			//关闭服药判断
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}
}
/**
	* @brief  解析上报状态
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
static void flapp_parse_report_status(char *buf, u16 len)
{
	u16 read_buf[4];
	if(strstr(buf,"\"status\":")){	//iccid字符串
		if(flapp_net_send.u8CurNetStep == AT_RPT_MED_READ)	//上传服用计划标记当天服用完成
		{
			http_ctrl.bTodMedInfo = TRUE;
			flash_read(FLASH_MED_LOG_ADDR,read_buf,4);	//读取未上报服药记录
			if(FLASH_SAVE_HEAD_ADDR == (read_buf[0] << 16) + read_buf[1])
			{
				read_buf[0] = 0;
				read_buf[1] = 0;
				flash_write(FLASH_MED_LOG_ADDR,read_buf,4);
			}
		}
		flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}
}
/**
	* @brief  解析获取ICCID用于本地保存
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
static void flapp_parse_get_iccid(char *buf, u16 len)
{
	char *p;
	flapp_sys_para_t sys_id_para;
	if(strstr(buf,"+ICCID:")){	//iccid字符串
		sys_id_para = flapp_get_sys_para();
		p = strchr(buf,' ') + 1;
		memset(flapp_net_para.s8Iccid, 0, ICCD_MAX_LEN);
		strncpy(flapp_net_para.s8Iccid,p,ICCD_MAX_LEN);
		flapp_net_para.s8Iccid[ICCD_MAX_LEN] = '\0';
		
		if(flapp_net_send.u8CurNetStep > AT_REF_TOKEN_TERM)
			flapp_net_send.u8CurNetStep = flapp_net_para.u8LastNetStep;
		else
			flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
		
		if(sys_id_para.bTestMode)	//测试模式下
		{
			if(strlen(flapp_net_para.s8Iccid) == ICCD_MAX_LEN){	//ICCID正确亮充电灯
				LED_LOW_POW_ON;
				LED_CHAR_FULL_ON;
			}else{
				LED_LOW_POW_OFF;
				LED_CHAR_FULL_OFF;
			}	
		}
		COM_PRINTF("iccid:%s len:%d\r\n",flapp_net_para.s8Iccid,strlen(flapp_net_para.s8Iccid));
	}
}

/**
* @brief  解析出信号强度
  * @param  buf：解析字符串	len：字符串长度
  * @retval none
  */
static void flapp_parse_get_csq(char *buf, u16 len)
{
	char *p;
	p = strstr(buf,"CSQ: ");
	if(p){
		flapp_net_para.u8ModCsq = flapp_get_dec_num_from_str(p+strlen("CSQ: "),',');
		log_debug(FACTORY_CSQ,flapp_net_para.u8ModCsq * 10);
		if(flapp_net_send.u8CurNetStep > AT_REF_TOKEN_TERM)
			flapp_net_send.u8CurNetStep = flapp_net_para.u8LastNetStep;
		else
			flapp_net_send.u8CurNetStep++;
		flapp_timeout_fun(TIMEOUT_CLR);
	}
}

/**
	* @brief  网络连接中运行步骤
	* @param  void
  * @retval none
  */
static void flapp_net_connecting_run(void)
{
	if(flapp_net_send.u16CurRunTime < flapp_uart_send[flapp_net_send.u8CurNetStep].u16Time){
		flapp_net_send.u16CurRunTime++;
		return;
	}
	else if(flapp_net_send.u16CurRunTime != TIME_WAIT_REQ)			//超时无响应
	{
		flapp_timeout_fun(TIMEOUT_ADD_RUN);
	}
	if(flapp_net_send.u8CurNetStep > AT_STEP_SET_CSCLK)	
		return;

	flapp_net_send.u16CurRunTime = FALSE;
	fldrv_uart_send_buf(flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,strlen(flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend));
}
/**
	* @brief  网络连接成功运行步骤
	* @param  void
  * @retval none
  */
static void flapp_net_connected_run(void)
{
	u8 i;
	flapp_sys_para_t sys_net_para;
	sys_net_para = flapp_get_sys_para();
	if(NET_WAIT == flapp_net_send.u8CurNetStep)
	{
		if(flapp_net_para.u32HttpReqFlag == FALSE)	return;

		for(i = 0; i < API_HTTP_MAX; i++)
		{
			if(GETBIT_VAL(flapp_net_para.u32HttpReqFlag, i))
			{
				flapp_net_send.u8CurNetStep = net_connected_req_step[i];
				flapp_net_send.u8CurHttpStep = i;
				break;
			}
		}
	}

	if(flapp_net_send.u16CurRunTime < flapp_uart_send[flapp_net_send.u8CurNetStep].u16Time){
		flapp_net_send.u16CurRunTime++;
		return;
	}
	else if(flapp_net_send.u16CurRunTime != TIME_WAIT_REQ)			//超时无响应
	{
		flapp_timeout_fun(TIMEOUT_ADD_RUN);
	}

	flapp_net_send.u16CurRunTime = FALSE;
	switch(flapp_net_send.u8CurNetStep){
		case AT_REF_TOKEN_AUTH:
		case AT_TOKEN_AUTH:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,HTTP_BASIC_CONTENT);
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		case AT_TOKEN_BODY_WAIT:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,HTTP_USER_NAME,HTTP_PASSWORD);
			strcat(flapp_net_send.s8AtSendBuf,EOF_BODY);	
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		case AT_REF_TOKEN_BODY_WAIT:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.s8RefreshToken);
			strcat(flapp_net_send.s8AtSendBuf,EOF_BODY);	
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		case AT_RPT_MED_BODY_WAIT:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.u32DvcID,eat_time.u16Year,eat_time.u8Mon,\
																													eat_time.u8Day,eat_time.u8Hour,eat_time.u8Min,eat_time.u8Sec,flapp_net_para.u8Interval);
			strcat(flapp_net_send.s8AtSendBuf,EOF_BODY);	
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		case AT_RPT_VOLUME_BODY_WAIT:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.u32DvcID,sys_net_para.u8VolLev);
			strcat(flapp_net_send.s8AtSendBuf,EOF_BODY);	
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		case AT_GET_MED_BODY_WAIT:
		case AT_RPT_CHECK_VIS_BODY_WAIT:
		case AT_RPT_SEEK_HELP_BODY_WAIT:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.u32DvcID);
			strcat(flapp_net_send.s8AtSendBuf,EOF_BODY);	
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		
		case AT_DVC_PARA_AUTH:
		case AT_GET_MED_SET_AUTH:
		case AT_GET_RING_LEN_AUTH:
		case AT_GET_VOLUME_AUTH:
		case AT_GET_SUB_DATE_AUTH:
		case AT_GET_TOD_MED_AUTH:
		case AT_GET_SUB_CHECK_AUTH:
		case AT_RPT_MED_AUTH:
		case AT_RPT_VOLUME_AUTH:
		case AT_RPT_CHECK_VIS_AUTH:
		case AT_RPT_SEEK_HELP_AUTH:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.s8AccessToken);
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		case AT_DVC_PARA_BODY_WAIT:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.u32DvcID,flapp_net_para.s8Iccid,VERSION,sys_net_para.bHaveUsb,sys_net_para.bPowOnOff,sys_net_para.u8BatEle*10,flapp_net_para.u8ModCsq*10,flapp_net_para.u8ApiStatus);
			strcat(flapp_net_send.s8AtSendBuf,EOF_BODY);	//添加body结束符
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;
		//上传URL加ID的
		case AT_GET_TIME_URL:
		case AT_GET_MED_SET_URL:
		case AT_GET_RING_LEN_URL:
		case AT_GET_VOLUME_URL:
		case AT_GET_SUB_DATE_URL:
		case AT_GET_TOD_MED_URL:
//		case AT_GET_SUB_CHECK_URL:
			sprintf(flapp_net_send.s8AtSendBuf,flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,flapp_net_para.u32DvcID);
			fldrv_uart_send_buf(flapp_net_send.s8AtSendBuf,strlen(flapp_net_send.s8AtSendBuf));
			break;

		default:
			fldrv_uart_send_buf(flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,strlen(flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend));
			break;	
	}
}
/**
	* @brief  根据当前网络状态运行相关步骤
	* @param  void
  * @retval none
  */
void flapp_net_send_slm_run(void)
{
	if(flapp_net_send.u8TimeOutSta == TIMEOUT_RES_RUN)			//超时等待重启
	{
		flapp_timeout_fun(TIMEOUT_RES_RUN);
		return;
	}
	
	switch(flapp_net_para.u8NetSta){		//根据网络状态执行不同分支
		case NET_STA_CONNECTING:
			flapp_net_connecting_run();
			break;
		case NET_STA_CONNECTED_SUCC:
			flapp_net_connected_run();
			break;
		case NET_STA_CONNECTED_FAIL:
			break;
		default:break;
	}
}

/**
	* @brief  解析网络模组数据
	* @param  void
  * @retval none
  */
void flapp_net_rcv_slm_run(void)
{
	fldrv_uart_rx_t tNetRcvBuf;
	tNetRcvBuf = fldrv_uart2_rcv_time_out_check();		//串口接收检测，接收超时就处理数据
	
	if(tNetRcvBuf.u16UartRxLen < UART_RCV_MIN_LEN)	return;

	if(strstr((char*)tNetRcvBuf.u8UartRxBuf,"AT READY") || \
		 strstr((char*)tNetRcvBuf.u8UartRxBuf,"+SIM Ready"))	//模块准备好，重置步骤
	{
		flapp_net_send.u8CurNetStep = AT_STEP_GET_ICCID;
		flapp_net_send.u16CurRunTime = 0xFFFF;
		return;
	}
	else if(strstr((char*)tNetRcvBuf.u8UartRxBuf,"1 401")  || 
					strstr((char*)tNetRcvBuf.u8UartRxBuf,"+CME ERROR: 53"))	//401token无效，ERROR 53参数错误重置token
	{
		flapp_set_http_req(API_GET_TOKEN,API_STATUS_MAX);							//获取token
		flapp_timeout_fun(TIMEOUT_ADD_RUN);
		return;
	}
	else if(strstr((char*)tNetRcvBuf.u8UartRxBuf,"+CME ERROR") || \
					strstr((char*)tNetRcvBuf.u8UartRxBuf,"1 402")	|| \
					strstr((char*)tNetRcvBuf.u8UartRxBuf," 500 ")	|| \
					strstr((char*)tNetRcvBuf.u8UartRxBuf," 502 ") || \
					strstr((char*)tNetRcvBuf.u8UartRxBuf,"please exe AT+HTTPTERM first"))
	{
		flapp_timeout_fun(TIMEOUT_ADD_RUN);
		return;
	}

	if(flapp_net_send.u8CurNetStep < NET_WAIT){								//只对有效步骤进行解析
		flapp_net_send.u8CurNetConCount = RESET;
		flapp_uart_send[flapp_net_send.u8CurNetStep].pFunc((char*)tNetRcvBuf.u8UartRxBuf, tNetRcvBuf.u16UartRxLen);
	}
}
/**
	* @brief  加载请求的API参数
	* @param  void
  * @retval none
  */
void flapp_set_http_req(fldrv_api_http_req_e var,fldrv_api_status_req_e api)
{
	if(flapp_net_para.bHaveID == FALSE)
		return;
	if((api == API_KEY_POWER_OFF) || (api == API_LOW_POW_POWER_OFF))	//关机中断网络请求指令，只做设备参数上报
	{
		flapp_net_para.u32HttpReqFlag &= (API_GET_TOKEN + 1);
	}
	SETBIT_VAL(flapp_net_para.u32HttpReqFlag,var);
	if(api < API_STATUS_MAX)
		flapp_net_para.u8ApiStatus = api;
}
/**
	* @brief  获取信号强度，厂测用
	* @param  void
  * @retval none
  */
u8 flapp_get_csq_status(void)
{
	return flapp_net_para.u8ModCsq;
}
/**
	* @brief  获取id
	* @param  void
  * @retval none
  */
u32 flapp_get_id_status(void)
{
	return flapp_net_para.u32DvcID;
}
/**
	* @brief  获取http请求状态
	* @param  void
  * @retval none
  */
u32 flapp_get_http_status(void)
{
	return flapp_net_para.u32HttpReqFlag;
}

u8 flapp_get_net_status(void)
{
	return flapp_net_para.u8NetSta;
}
/**
	* @brief  插入查询指令，厂测用
	* @param  void
  * @retval none
  */
void flapp_insert_inquire_cmd(u8 step)
{
	flapp_net_para.u8LastNetStep = flapp_net_send.u8CurNetStep;
	flapp_net_send.u8CurNetStep = step;
	flapp_net_send.u16CurRunTime = FALSE;
	fldrv_uart_send_buf(flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend,strlen(flapp_uart_send[flapp_net_send.u8CurNetStep].s8AtSend));
}
/**
	* @brief  获取复诊参数，复诊提醒用
	* @param  void
  * @retval none
  */
void flapp_get_sub_para_ctrl(sub_para_t* para)
{
	para->u16Year = sub_day.u16Year;
	para->u8Mon = sub_day.u8Mon;
	para->u8Day = sub_day.u8Day;
	para->bTodInfo = http_ctrl.bTodReferralInfo;
	para->bTimeAdj = http_ctrl.bTimeAdj;
}
/**
	* @brief  设置当前服药时间
	* @param  void
  * @retval none
  */
void flapp_set_cur_med_time(void)
{
	ertc_time_type cur_time;
	ertc_calendar_get(&cur_time);
	eat_time.u16Year = cur_time.year + 2000;
	eat_time.u8Mon = cur_time.month;
	eat_time.u8Day = cur_time.day;
	eat_time.u8Hour = cur_time.hour;
	eat_time.u8Min = cur_time.min;
	eat_time.u8Sec = cur_time.sec;
}
/**
	* @brief  关机保存服药记录
	* @param  void
  * @retval none
  */
void flapp_offline_save_med_log(void)
{
	u32 res;
	if(GETBIT_VAL(flapp_net_para.u32HttpReqFlag, API_RPT_MED_TIME))	//如果服药记录上传没有成功
	{
		u16 med_log_buf[4];
		res = covBeijing2UnixTimeStp(&eat_time);
		COM_PRINTF("power off med info:%d",res);
		med_log_buf[0] = (u32)FLASH_SAVE_HEAD_ADDR >> 16;
		med_log_buf[1] = (u16)FLASH_SAVE_HEAD_ADDR;
		med_log_buf[2] = res >> 16;
		med_log_buf[3] = res;
		flash_write(FLASH_MED_LOG_ADDR,med_log_buf,4);
	}
}

/**
* @brief  隔天复位http接口的服药状态
	* @param  void
  * @retval none
  */
void flapp_reset_tod_med_info(void)
{
	u16 read_buf[4];	
	flash_read(FLASH_MED_LOG_ADDR,read_buf,4);	//读取未上报服药记录
	if(FLASH_SAVE_HEAD_ADDR == (read_buf[0] << 16) + read_buf[1])	//隔天如果还没有上报清除
	{
		read_buf[0] = 0;
		read_buf[1] = 0;
		flash_write(FLASH_MED_LOG_ADDR,read_buf,4);
	}
	http_ctrl.bTodMedInfo = FALSE;
	CLRBIT_VAL(flapp_net_para.u32HttpReqFlag,API_RPT_MED_TIME);	//隔天清除当前服药上报接口
}
